home *** CD-ROM | disk | FTP | other *** search
- /* UnIX CD-ROM symbolic link utility by
- Matthew B. Hornbeck, Director of Technical Services
- Copyright 1989 by Young Minds, Incorporated
- June 19, 1989.
- File : CD_LINK.C
- */
-
- #ifdef sun
- #define REALPATH
- #endif
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <malloc.h>
-
- #ifdef REALPATH
- #include <sys/param.h>
- #else
- #define MAXPATHLEN 1024
- #endif
-
- #define FALSE 0
- #define TRUE !FALSE
-
- char *prog_name (program_name)
- char *program_name;
-
- { unsigned int len;
-
- len = strlen (program_name) - 1;
- program_name = program_name + len;
- while ((len != 0) && (*program_name != '/'))
- { program_name --;
- len --;
- }
- if (*program_name == '/')
- program_name ++;
- return program_name;
- }
-
- typedef struct dir_element {
- struct dir_element *next;
- char *cd_path;
- char *new_path;
- } dir_elem;
-
- static dir_elem *head = NULL, *tail = NULL;
-
- int push_dir (cd_path, new_path)
- char *cd_path, *new_path;
-
- { dir_elem *tmp;
-
- if (head == NULL)
- { if ((head = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
- { fprintf (stderr, "Unable to allocate dir_element buffer!\n");
- return FALSE;
- }
- }
- else
- { if ((tail->next = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
- { fprintf (stderr, "Unable to allocate dir_element buffer!\n");
- return FALSE;
- }
- }
- tmp->next = NULL;
- if ((tmp->cd_path = (char *) malloc (strlen (cd_path) + 1)) == NULL)
- { fprintf (stderr, "Unable to allocate cd_path of dir_element!\n");
- return FALSE;
- }
- strcpy (tmp->cd_path, cd_path);
- if ((tmp->new_path = (char *) malloc (strlen (new_path) + 1)) == NULL)
- { fprintf (stderr, "Unable to allocate new_path of dir_element!\n");
- return FALSE;
- }
- strcpy (tmp->new_path, new_path);
- tail = tmp;
- return TRUE;
- }
-
- dir_elem *dequeue_dir ()
-
- { dir_elem *tmp;
-
- if (head == NULL)
- { tail = NULL;
- return NULL;
- }
- tmp = head;
- head = tmp->next;
- tmp->next = NULL;
- return tmp;
- }
-
- free_dir (dir)
- dir_elem *dir;
-
- {
- free (dir->cd_path);
- free (dir->new_path);
- free (dir);
- }
-
- int proc_dir (cd_path, new_path, recurse)
- char *cd_path, *new_path;
- int recurse;
-
- { FILE *fp;
- char line [4096], link_name [4096], new_name [4096];
- char ymtrans_name [4096], resolved_name [MAXPATHLEN];
- char type, elem1 [65], elem2 [2048], elem3 [2048];
- int line_cnt, elem_cnt, i, j;
-
- sprintf (ymtrans_name, "%s/ymtrans.tbl", cd_path);
- if ((fp = fopen (ymtrans_name, "rt")) == NULL)
- { fprintf (stderr, "Unable to open file %s!\n", ymtrans_name);
- return FALSE;
- }
- line_cnt = 0;
- while (fgets (line, sizeof (line), fp) != NULL)
- { line_cnt ++;
- if ((strlen (line) < 17) || (line [1] != ' ') || (line [strlen (line) - 1] != '\n'))
- { fprintf (stderr, "Invalid %s file!?!\n", ymtrans_name);
- exit (1);
- }
- type = line [0];
- for (i = 2, j = 0; (line [i] != ' ') && (line [i] != '\t'); i ++, j ++)
- if (isupper (line [i]))
- elem1 [j] = tolower (line [i]);
- else
- elem1 [j] = line [i];
- elem1 [j] = '\0';
- for (i = 15, j = 0; (line [i] != '\t') && (line [i] != '\n'); i ++, j ++)
- elem2 [j] = line [i];
- elem2 [j] = '\0';
- elem_cnt = 2;
- j = 0;
- if (line [i] == '\t')
- { for (i ++; line [i] != '\n'; i ++, j ++)
- elem3 [j] = line [i];
- elem_cnt ++;
- }
- elem3 [j] = '\0';
- if ((line_cnt == 1) && (strcmp (elem1, ".") == 0))
- continue;
- if ((line_cnt == 2) && (strcmp (elem1, "..") == 0))
- continue;
- sprintf (new_name, "%s/%s", new_path, elem2);
- switch (type)
- { case 'F' : sprintf (link_name, "%s/%s", cd_path, elem1);
- if (symlink (link_name, new_name) != 0)
- fprintf (stderr, "Unable to make link %s to %s!\n", elem1, elem2);
- break;
-
- case 'L' : if (symlink (elem3, new_name) != 0)
- fprintf (stderr, "Unable to make link %s to %s!\n", elem1, elem3);
- break;
-
- case 'D' : mkdir (new_name, 0777);
- if (recurse)
- { sprintf (link_name, "%s/%s", cd_path, elem1);
- push_dir (link_name, new_name);
- }
- break;
-
- case 'M' : mkdir (new_name, 0777);
- if (recurse)
- { for (i = 0; elem3 [i] != '\0'; i ++)
- if (isupper (elem3 [i]))
- elem3 [i] = tolower (elem3 [i]);
- sprintf (link_name, "%s/%s", cd_path, elem3);
-
- #ifdef REALPATH
- realpath (resolved_name, link_name);
- #else
- strcpy (link_name, resolved_name);
- #endif
-
- push_dir (resolved_name, new_name);
- }
- break;
-
- case '#' : break;
- }
- }
- fclose (fp);
- return TRUE;
- }
-
- int main (argc, argv)
- int argc;
- char *argv [];
-
- { dir_elem *cur_dir;
- char cd_pathname [2048], resolved_name [MAXPATHLEN];
- int recurse;
-
- recurse = FALSE;
- if ((argc == 3) && ((strcmp (argv [1], "-r") == 0) || (strcmp (argv [1], "-R") == 0)))
- recurse = TRUE;
- else if (argc != 2)
- { fprintf (stderr, "Usage : %s [-r] cd_pathname\n", prog_name (argv [0]));
- exit (1);
- }
- if (argv [argc - 1] [0] == '/')
- strcpy (cd_pathname, argv [argc - 1]);
- else
- { getwd (cd_pathname);
- strcat (cd_pathname, "/");
- strcat (cd_pathname, argv [argc - 1]);
- }
-
- #ifdef REALPATH
- realpath (cd_pathname, resolved_name);
- #else
- strcpy (resolved_name, cd_pathname);
- #endif
-
- push_dir (resolved_name, ".");
- while ((cur_dir = dequeue_dir ()) != NULL)
- { proc_dir (cur_dir->cd_path, cur_dir->new_path, recurse);
- free_dir (cur_dir);
- }
- return 0;
- }
-